#!/usr/bin/env python
# coding=utf-8

from toughadmin.tools.utils import safestr
from toughadmin.console.admin.api import ApiHandler
from toughadmin.tools.permit import permit
from toughadmin.console import models


class SessionQueryHandler(ApiHandler):

    def post(self):
        try:
            req_msg = self.parse_request()
        except Exception as err:
            self.render_json(code=1, msg=safestr(err.message))
            return

        session_id = req_msg.get("session_id")
        if not session_id:
            self.render_json(code=1, msg="session_id is empty")

        session = self.db.query(models.TraSession).filter_by(session_id=session_id)

        result = dict(
            username=session.username,
            nas_addr=session.nas_addr,
            session_id=session_id,
            start_time=session.start_time,
            ipaddr=session.ipaddr,
            macaddr=session.macaddr,
            input_total=session.input_total,
            output_total=session.output_total
        )

        self.render_json(code=0, msg="success", data=result)


class SessionExistsHandler(ApiHandler):

    def post(self):
        try:
            req_msg = self.parse_request()
        except Exception as err:
            self.render_json(code=1, msg=safestr(err.message))
            return

        session_id = req_msg.get("session_id")
        if not session_id:
            self.render_json(code=1, msg="session_id is empty")

        _exists = self.db.query(models.TraSession.id).filter_by(session_id=session_id).count() > 0

        if _exists:
            self.render_json(code=0, msg="session exists")
        else:
            self.render_json(code=1, msg="session not exists")


class SessionAddHandler(ApiHandler):

    def post(self):
        try:
            req_msg = self.parse_request()
        except Exception as err:
            self.render_json(code=1, msg=safestr(err.message))
            return

        try:
            session = models.TraSession()
            session.username = req_msg['username']
            session.nas_addr = req_msg['nas_addr']
            session.session_id = req_msg['session_id']
            session.start_time = req_msg['start_time']
            session.ipaddr = req_msg['ipaddr']
            session.macaddr = req_msg['macaddr']
            session.input_total = req_msg['input_total']
            session.output_total = req_msg['output_total']
            self.db.add(session)
            self.db.commit()
            self.render_json(code=0, msg="success")

        except Exception as err:
            self.render_json(code=1,msg=safestr(err))


class SessionDeleteHandler(ApiHandler):

    def post(self):
        try:
            req_msg = self.parse_request()
        except Exception as err:
            self.render_json(code=1, msg=safestr(err.message))
            return

        session_id = req_msg.get("session_id")
        if not session_id:
            self.render_json(code=1, msg="session_id is empty")

        self.db.query(models.TraSession).filter_by(session_id=session_id).delete()
        self.render_json(code=0, msg="success")


permit.add_handler(SessionQueryHandler, r"/api/session/query")
permit.add_handler(SessionExistsHandler, r"/api/session/exists")
permit.add_handler(SessionAddHandler, r"/api/session/add")
permit.add_handler(SessionDeleteHandler, r"/api/session/delete")